package com.hbhmzy.call;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.content.LocalBroadcastManager;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TextView;

import com.hbhmzy.utils.Client;

public class WaitCallActivity extends Activity implements View.OnClickListener {
    private String symbol,targetname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_waitcall);
        rejectbroadcast();
        init();
    }

    private void init() {
        //获取从MainActivity传过来的symbol；
        Intent intent=getIntent();
        Bundle bundle=intent.getExtras();
        symbol=bundle.getString("symbol");
        targetname=symbol.substring(symbol.length()-12,symbol.length()-1);
        TextView textView=findViewById(R.id.callwait);
        ImageView acc_btn=findViewById(R.id.acc);
        ImageView rej_btn=findViewById(R.id.rej);
        textView.setText(targetname);
        acc_btn.setOnClickListener(this);
        rej_btn.setOnClickListener(this);
    }

    private void rejectbroadcast() {
        //注册本地广播接收器 接收"hbhmzy.reject"类型广播
        IntentFilter intentFilter_rej1 = new IntentFilter();
        intentFilter_rej1.addAction("hbhmzy.reject");
        LocalReceiver_rej1 localReceiver_rej1 = new LocalReceiver_rej1();
        getApplication().registerReceiver(localReceiver_rej1, intentFilter_rej1);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.acc:
            {
                Client.send(symbol+"acc");
                Intent intent=new Intent(WaitCallActivity.this,CallingActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("symbol",symbol);
                bundle.putBoolean("is_online",true);
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
                break;
            }
            case R.id.rej:
            {
                Client.send(symbol+"reject");
                finish();
                break;
            }
            default: break;
        }

    }

    //定义广播接收器
    //当对方挂断的时候 跳转到主页面
    class LocalReceiver_rej1 extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
        }
    }


}
